package services;

import tw.edu.tku.im.maze.DeliveryListenerI;
import tw.edu.tku.im.maze.LoginDeliverer;
import tw.edu.tku.im.maze.LoginRep;
import tw.edu.tku.im.maze.LoginReq;
import tw.edu.tku.im.maze.RepPacket;
import tw.edu.tku.im.maze.TimeOutRep;

import com.example.maze.ConnectionInfo;
import com.example.maze.LobbyActivity;
import com.example.maze.MyLog;
import com.example.maze.R;

import android.app.AlertDialog;
import android.app.IntentService;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import com.example.maze.MyProgressDialog;


public class LoginService extends IntentService 
                          implements DeliveryListenerI {
   //[ ---------- Constant ----------
   public static final int LOGIN_REP_STATUS_SUCCESS= 0,
                           LOGIN_REP_STATUS_ERROR_ID= 1,
                           LOGIN_REP_STATUS_ERROR_PWD= 2,
                           CONNECTION_TIMEOUT        =-1;
   public static final String BROADCAST_ACTION="LOGIN_REP",
                              STATUS="status",
                              PACKET="packet";
   //] ---------- Constant ----------  
   public LoginService()            { super("LoginService"); }  
   public LoginService(String name) { super(name); }
   
//   private static MyProgressDialog mProgressDialog;
   @Override
   protected void onHandleIntent(Intent intent) {
      String id = intent.getStringExtra("id");
      String pwd= intent.getStringExtra("pwd");
//      mProgressDialog= (MyProgressDialog)intent.getParcelableExtra("progressDialog");
////      mProgressDialog.setCancelable(true);
////      mProgressDialog.setIndeterminate(false);
//      mProgressDialog.show("","帳號驗證中, 請稍候");      
      try{
         //[ 供單機測試
         if(id.equals("liawgrp") && pwd.equals("liawgrp")) { 
            Intent localIntent= new Intent(BROADCAST_ACTION);
            localIntent.putExtra(STATUS, LOGIN_REP_STATUS_SUCCESS);
            localIntent.putExtra(PACKET, new LoginRep(0));
            ConnectionInfo.identity= ConnectionInfo.HE_IS_HOST;
            LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
         } else {
            LoginDeliverer mDeliverer= new LoginDeliverer();
            mDeliverer.deliver(new LoginReq(id, pwd), this);            
         }
      }catch(Exception e) { 
         android.util.Log.d("DebugMsg",e.getMessage()); 
      }
   }
   // -------------------------------------------------- 
   @Override
   public void processReply(RepPacket rep) {
      Intent localIntent= new Intent(BROADCAST_ACTION);  
      
      if(rep instanceof TimeOutRep) {
         localIntent.putExtra(STATUS, CONNECTION_TIMEOUT);         
      } else {
         LoginRep mLoginRep= (LoginRep)rep;          
         switch(mLoginRep.getState()) {
            case LOGIN_REP_STATUS_SUCCESS: 
               localIntent.putExtra(STATUS, LOGIN_REP_STATUS_SUCCESS);
               localIntent.putExtra(PACKET, mLoginRep);
               break;
            case LOGIN_REP_STATUS_ERROR_ID: 
               localIntent.putExtra(STATUS, LOGIN_REP_STATUS_ERROR_ID);
               break;
            case LOGIN_REP_STATUS_ERROR_PWD:             
               localIntent.putExtra(STATUS, LOGIN_REP_STATUS_ERROR_PWD);
               break;
            default: 
               //[ 可能發生未在掌控之內的不明例外
               //localIntent.putExtra(STATUS, CONNECTION_TIMEOUT);
         }
      }
      LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
   }
}
